local GetVendored = DongleStub('Dongle-1.1'):New('GetVendored')
local db
local databasedefaults = {
	profile = {
		filter = {},
		ignore = {},
		enabled = true,
		output = true,
	}
}

function GetVendored:Initialize()	
	self.db = self:InitializeDB('GetVendoredDB', databasedefaults)
	self.slashcmd = self:InitializeSlashCommand('GetVendored Slash Commands','GetVendored', 'junk', 'getvendored')
end

function GetVendored:Enable()
	db = self.db.profile	
	self.slashcmd:RegisterSlashHandler('Add: Adds an item to the filter', '^add (.+)$','AddFilter')
	self.slashcmd:RegisterSlashHandler('Remove: Removes an item from the filter', '^remove (.+)$','RemoveFilter')
	self.slashcmd:RegisterSlashHandler('Ignore: Add or remove and item to the ignore list so it wont be vendored', '^ignore (.+)$','Ignore')
	self.slashcmd:RegisterSlashHandler('List: Lists the current filter lsit', '^list$','Listdb')
	self.slashcmd:RegisterSlashHandler('Clear: Clears the filter (Warning: Cannot be undone)', '^clear$','Clear')
	GetVendored:RegisterEvent('MERCHANT_SHOW', 'Vendor')
end
--Opens at vendors
function GetVendored:Vendor()
    for bags=0,4 do
		for slot=0,GetContainerNumSlots(bags) do
			local itemlink = (GetContainerItemLink(bags, slot))
			if itemlink then
				self:Check(bags, slot, itemlink)
				self:FilterCheck(bags, slot, itemlink)
			end
		end
	end
end
--Checks bags for grey items and vendors them
function GetVendored:Check(bags, slot, itemlink)
	if select(3, GetItemInfo(itemlink)) == 0 and not db.ignore[self:LinkToStr(itemlink)] then
		UseContainerItem(bags, slot)
		self:Print("Vendoring: "..self:LinkToStr(itemlink))
	end
end
--Checks itemlink in filter
function GetVendored:FilterCheck(bags, slot, itemlink)
	if db.filter[self:LinkToStr(itemlink)] and not db.ignore[self:LinkToStr(itemlink)] then
		UseContainerItem(bags, slot)
		self:Print("Vendoring: "..self:LinkToStr(itemlink))
	end		
end
--Makes links into text
function GetVendored:LinkToStr(itemlink)
	return strtrim(select(1, (GetItemInfo(string.match(itemlink, "item:(%d+)")))))
end
--Adds items the databsae
function GetVendored:AddFilter(item)
	local item = self:LinkToStr(item)
	if not db.filter[item] then
		db.filter[item] = true
		self:Print(item.." added to filter")
	else
		self:Print("Thats already in the filter")
	end
end	
--Remove from the database
function GetVendored:RemoveFilter(item)
	local item = self:LinkToStr(item)
	if db.filter[item] then
		db.filter[item] = nil
		self:Print(item.." Removed from filter")
	end
end
--List lol
function GetVendored:Listdb()
	self:Print("Current ignore list:")
	for k,v in pairs(db.ignore) do self:Print(k) end
	self:Print("Current Filter list:")
	for k,v in pairs(db.filter) do self:Print(k) end
end
--Clear
function GetVendored:Clear()
	for k,v in pairs(db.filter) do db[k] = nil end
	self:Print("Filter cleared")
end
--Ingore
function GetVendored:Ignore(item)
	local item = self:LinkToStr(item)
	if not db.filter[item] and not db.ignore[item] then
		db.ignore[item] = true
	else
		db.ignore[item] = nil
	end
end